<template>
  <div class="divBox listPla">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-tabs
          v-model="tableFrom.type"
          @tab-click="getList(1), getLstFilterApi()"
        >
          <el-tab-pane
            v-for="(item, index) in headeNum"
            :key="index"
            :name="item.type.toString()"
            :label="item.name + '(' + item.count + ')'"
          />
        </el-tabs>
        <div class="container">
          <el-form size="small" :inline="true">
            <el-form-item :label="$t('platformcommodity')">
              <el-cascader
                v-model="tableFrom.cate_id"
                class="selWidth"
                :options="categoryList"
                :props="props"
                clearable
                @change="getList(1)"
              />
            </el-form-item>
            <el-form-item :label="$t('commodityclass')">
              <el-select
                v-model="tableFrom.mer_cate_id"
                :placeholder="$t('pleaseselect')"
                class="filter-item selWidth mr20"
                clearable
                @change="getList(1)"
              >
                <el-option
                  v-for="item in merCateList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item :label="$t('keywordsearch')">
              <el-input
                v-model="tableFrom.keyword"
                :placeholder="$t('Please4')"
                class="selWidth"
                @keyup.enter.native="getList(1)"
              >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  class="el-button-solt"
                  @click="getList(1)"
                />
              </el-input>
            </el-form-item>
          </el-form>
        </div>
        <el-button size="small" type="success" @click="importPlatfrom">{{
          $t("oneclickdistribution")
        }}</el-button>
      </div>
      <div class="order_list_mobile" v-if="clientWidth < 1024">
        <div class="item" v-for="(item, index) in tableData.data" :key="index">
          <div class="item_top">
            <div class="item_row">
              <div class="product_id">ID:{{ item.product_id }}</div>
              <div class="status">
                {{ item.us_status | productStatusFilter }}
              </div>
            </div>
            <div class="create_time">
              {{ $t("creationtime") }}{{ item.create_time }}
            </div>
          </div>
          <div class="item_middle">
            <div class="info">
              <div class="demo-image__preview">
                <el-image :src="item.image" :preview-src-list="[item.image]" />
                <span class="tabBox_tit">
                  {{ item.store_name }}
                </span>
              </div>
            </div>
            <div class="item_switch">
              <el-switch
                v-model="item.is_show"
                :active-value="1"
                :inactive-value="0"
                :active-text="$t('putonshelves')"
                :inactive-text="$t('lowershelf')"
                @change="onchangeIsShow(item)"
              />
            </div>

            <div class="desc">
              <span
                >{{ $t("commodityprice") }}<em>{{ item.price }}</em>
              </span>
              <span>
                {{ $t("buying.price") }} <em>{{ item.jh_price }}</em>
              </span>
            </div>

            <div class="desc">
              <span>{{ $t("stock") }} ：{{ item.stock }}</span>
              <span> {{ $t("salesvolume") }}： {{ item.sales }}</span>
            </div>
          </div>

          <div class="item_bottom">
            <router-link
              v-if="tableFrom.type != 5"
              :to="{
                path:
                  roterPre + '/product/reviews/?product_id=' + item.product_id
              }"
            >
              <el-button type="text" size="small" class="mr10">{{
                $t("evaluate")
              }}</el-button>
            </router-link>
          </div>
        </div>
      </div>
      <div class="order_list_pc" v-if="clientWidth > 1024">
        <el-table
          v-loading="listLoading"
          :data="tableData.data"
          style="width: 100%"
          size="mini"
          :row-class-name="tableRowClassName"
          @rowclick.stop="closeEdit"
        >
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-form
                label-position="left"
                inline
                class="demo-table-expand demo-table-expand1"
              >
                <el-form-item :label="$t('platformclassification')">
                  <span>{{
                    props.row.storeCategory
                      ? props.row.storeCategory.cate_name
                      : "-"
                  }}</span>
                </el-form-item>
                <el-form-item :label="$t('commodityclassification')">
                  <template v-if="props.row.merCateId.length">
                    <span
                      v-for="(item, index) in props.row.merCateId"
                      :key="index"
                      class="mr10"
                      >{{ item.category?item.category.cate_name:"-" }}</span>
                  </template>

                  <span v-else>-</span>
                </el-form-item>
                <el-form-item :label="$t('brand')">
                  <span class="mr10">{{
                    props.row.brand ? props.row.brand.brand_name : "-"
                  }}</span>
                </el-form-item>
                <el-form-item :label="$t('marketprice')">
                  <span>{{ props.row.ot_price | filterEmpty }}</span>
                </el-form-item>
                <el-form-item :label="$t('costprice')">
                  <span>{{ props.row.cost | filterEmpty }}</span>
                </el-form-item>
                <el-form-item :label="$t('collection')">
                  <span>{{ props.row.care_count | filterEmpty }}</span>
                </el-form-item>
                <el-form-item
                  v-if="tableFrom.type === '7'"
                  key="1"
                  :label="$t('reasonforfailure')"
                >
                  <span>{{ props.row.refusal }}</span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column prop="product_id" label="ID" min-width="50" />
          <el-table-column :label="$t('commoditymap')" min-width="80">
            <template slot-scope="scope">
              <div class="demo-image__preview">
                <el-image
                  :src="scope.row.image"
                  :preview-src-list="[scope.row.image]"
                />
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="store_name"
            :label="$t('tradename')"
            min-width="200"
          />
          <el-table-column
            prop="price"
            :label="$t('sellingprice')"
            min-width="90"
          />
          <el-table-column
            prop="jh_price"
            :label="$t('buying.price')"
            min-width="100"
          />
          <el-table-column
            prop="sales"
            :label="$t('salesvolume')"
            min-width="90"
          />
          <el-table-column prop="stock" :label="$t('stock')" min-width="70" />
          <el-table-column
            v-if="Number(tableFrom.type) < 5"
            key="1"
            prop="status"
            :label="$t('upperowershelf')"
            min-width="150"
          >
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.is_show"
                :active-value="1"
                :inactive-value="0"
                :active-text="$t('putonshelves')"
                :inactive-text="$t('lowershelf')"
                @change="onchangeIsShow(scope.row)"
              />
            </template>
          </el-table-column>
          <el-table-column
            prop="create_time"
            :label="$t('creationtime')"
            min-width="110"
          />
          <el-table-column
            :label="$t('operation')"
            min-width="80"
            fixed="right"
          >
            <template slot-scope="scope">
              <router-link
                v-if="tableFrom.type != 5"
                :to="{
                  path:
                    roterPre +
                    '/product/reviews/?product_id=' +
                    scope.row.product_id
                }"
              >
                <el-button type="text" size="small" class="mr10">{{
                  $t("evaluate")
                }}</el-button>
              </router-link>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="block">
        <el-pagination
          :page-sizes="[20, 40, 60, 80]"
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
    <!-- 一件铺货 -->
    <el-dialog
      v-if="dialogLabelpla"
      :title="$t('oneclickdistribution')"
      :visible.sync="dialogLabelpla"
      z-index="2004"
      width="80%"
    >
      <div class="clearfix" style="margin: 10px auto">
        <el-button size="mini" type="primary" @click="batchpuhuo()">{{
          $t("confirmdistribution")
        }}</el-button>
      </div>
      <el-table
        ref="multipleSelection"
        v-loading="listLoadingPla"
        :row-key="
          row => {
            return row.product_id;
          }
        "
        :data="tableDataPla.data"
        style="width: 100%"
        size="mini"
        :row-class-name="tableRowClassName"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          :reserve-selection="true"
          width="55"
        />
        <el-table-column prop="product_id" label="ID" min-width="50" />
        <el-table-column :label="$t('commoditymap')" min-width="80">
          <template slot-scope="scope2">
            <div class="demo-image__preview">
              <el-image
                :src="scope2.row.image"
                :preview-src-list="[scope2.row.image]"
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="store_name"
          :label="$t('tradename')"
          min-width="240"
        />
        <el-table-column
          prop="price"
          :label="$t('commodityprice')"
          min-width="100"
        />
        <el-table-column
          prop="jh_price"
          :label="$t('buying.price')"
          min-width="100"
        />
        <el-table-column :label="$t('operation')" min-width="60" fixed="right">
          <template slot-scope="scope2">
            <el-button
              type="text"
              size="small"
              @click="puhuo(scope2.row.product_id)"
              >{{ $t("goodsdistribution") }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[20, 40, 60, 80]"
          :page-size="tableFromPla.limit"
          :current-page="tableFromPla.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableDataPla.total"
          @size-change="handleSizeChange2"
          @current-change="pageChange2"
        />
      </div>
    </el-dialog>
  </div>
</template>
<script>
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
import {
  paltformProduct,
  updatepaltformProduct,
  batctupdatepaltformProduct,
  productLstApi,
  productDeleteApi,
  categorySelectApi,
  lstFilterApi,
  statusApi,
  categoryListApi,
  destoryApi,
  restoreApi,
  productSort,
  getProductLabelApi,
  updatetProductLabel
} from "@/api/product";
import { roterPre } from "@/settings";
import previewBox from "@/components/previewBox/index";
export default {
  name: "ProductList",
  components: { previewBox },
  data() {
    return {
      props: {
        emitPath: false
      },
      clientWidth: "",
      roterPre: roterPre,
      headeNum: [],
      labelList: [],
      listLoading: true,
      tableData: {
        data: [],
        total: 0
      },
      tableFrom: {
        page: 1,
        limit: 20,
        mer_cate_id: "",
        cate_id: "",
        keyword: "",
        type: this.$route.query.type ? this.$route.query.type : "1",
        is_gift_bag: "",
        us_status: "",
        mer_labels: "",
        product_id: this.$route.query.id ? this.$route.query.id : ""
      },
      categoryList: [], // 平台
      merCateList: [], // 商户分类筛选
      modals: false,
      tabClickIndex: "",
      productStatusList: [
        { label: this.$t("displayonshelf"), value: 1 },
        { label: this.$t("lowershelf"), value: 0 },
        { label: this.$t("platformclosing"), value: -1 }
      ],
      goodsId: "",
      previewKey: "",
      product_id: "",
      labelForm: {},
      dialogLabelpla: !1,
      listLoadingPla: !1,
      tableDataPla: {
        data: [],
        total: 0
      },
      tableFromPla: {
        page: 1,
        limit: 20
      },
      ids: ""
    };
  },
  mounted() {
    this.clientWidth = document.body.clientWidth;
    this.getLstFilterApi();
    this.getCategorySelect();
    this.getCategoryList();
    this.getList(1);
    this.getLabelLst();
  },
  methods: {
    // 把每一行的索引放进row
    tableRowClassName({ row, rowIndex }) {
      row.index = rowIndex;
    },
    // 添加明细原因 row 当前行 column 当前列
    tabClick(row) {
      this.tabClickIndex = row.index;
    },
    // 失去焦点初始化
    inputBlur(scope) {
      if (!scope.row.sort || scope.row.sort < 0) scope.row.sort = 0;
      productSort(scope.row.product_id, { sort: scope.row.sort })
        .then(res => {
          this.closeEdit();
        })
        .catch(res => {});
    },
    closeEdit() {
      this.tabClickIndex = null;
    },
    getSuccess() {
      this.getLstFilterApi();
      this.getList(1);
    },
    getPaltfromProduct: function() {
      (this.listLoadingPla = !0),
        paltformProduct(this.tableFromPla)
          .then(res => {
            (this.tableDataPla.data = res.data.list),
              (this.tableDataPla.total = res.data.count),
              (this.listLoadingPla = !1);
          })
          .catch(res => {
            (this.listLoadingPla = !1), this.$message.error(res.message);
          });
    },
    // 复制淘宝
    importPlatfrom() {
      (this.dialogLabelpla = !0), this.getPaltfromProduct();
    },
    // 获取标签项
    getLabelLst() {
      getProductLabelApi()
        .then(res => {
          this.labelList = res.data;
        })
        .catch(res => {
          this.$message.error(res.message);
        });
    },
    // 商户分类；
    getCategorySelect() {
      categorySelectApi()
        .then(res => {
          this.merCateList = res.data;
        })
        .catch(res => {
          this.$message.error(res.message);
        });
    },
    // 平台分类；
    getCategoryList() {
      categoryListApi()
        .then(res => {
          this.categoryList = res.data;
        })
        .catch(res => {
          this.$message.error(res.message);
        });
    },
    // 列表表头；
    getLstFilterApi() {
      lstFilterApi()
        .then(res => {
          this.headeNum = res.data;
        })
        .catch(res => {
          this.$message.error(res.message);
        });
    },
    // 列表
    getList(num) {
      this.listLoading = true;
      this.tableFrom.page = num || this.tableFrom.page;
      productLstApi(this.tableFrom)
        .then(res => {
          this.tableData.data = res.data.list;
          this.tableData.total = res.data.count;
          this.listLoading = false;
        })
        .catch(res => {
          this.listLoading = false;
          this.$message.error(res.message);
        });
      this.getLstFilterApi();
    },
    pageChange(page) {
      this.tableFrom.page = page;
      this.getList("");
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val;
      this.getList("");
    },
    onchangeIsShow(row) {
      statusApi(row.product_id, row.is_show)
        .then(({ message }) => {
          this.$message.success(message);
          this.getList("");
          this.getLstFilterApi();
        })
        .catch(({ message }) => {
          this.$message.error(message);
        });
    },
    pageChange2(page) {
      this.tableFromPla.page = page;
      this.getPaltfromProduct("");
    },
    handleSizeChange2(val) {
      this.tableFromPla.limit = val;
      this.getPaltfromProduct("");
    },
    puhuo(id) {
      this.$modalForm(updatepaltformProduct(id)).then(() =>
        this.getPaltfromProduct()
      );
    },
    batchpuhuo() {
      this.$modalForm(batctupdatepaltformProduct({ ids: this.ids })).then(() =>
        this.getPaltfromProduct()
      );
    },
    toggleSection() {
      var t = this;
      this.tableDataPla.data.forEach(function(a) {
        t.$refs.multipleTable.toggleRowSelection(a);
      });
    },
    handleSelectionChange: function(t) {
      for (var a in ((this.ids = []), t)) this.ids.push(t[a].product_id);
    }
  }
};
</script>

<style scoped lang="scss">
.bg {
  z-index: 100;
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
}
.goods_detail .goods_detail_wrapper {
  z-index: -10;
}
::v-deep table.el-input__inner {
  padding: 0;
}
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand1 {
  ::v-deep label {
    width: 77px !important;
    color: #99a9bf;
  }
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 33.33%;
}
.selWidth {
  width: 350px !important;
}
.seachTiele {
  line-height: 35px;
}
</style>
